<?php
class B2b_Models_Dao_Pdo_Mysql_Blog extends Tomato_Model_Dao
    implements B2b_Models_Interface_Blog
{
    public function convert($entity)
    {
        return new B2b_Models_Blog($entity);
    }

    public function find($offset, $count, $exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('b' => $this->_prefix . 'enterprise_blog'))
                        ->where('b.language = ?', $this->_lang)
                        ;
        if ($exp) {
            if (isset($exp['enterprise'])) {
                $select->where($exp['enterprise']);
            }
			/*
				Name	: Cong Ngo, Date : 10:22 28/04/2011
				Purpose : Thêm điều kiện enterprise->frontend_active
						  +/ kết với bảng enterprise và thêm điều kiện frontend_active vào
			*/
			
            if (isset($exp['enterprise_frontend_active'])) {
				$select->joinLeft(array('e' => $this->_prefix.'enterprise'), "e.id = b.enterprise_id", array("id"));
                $select->where('e.frontend_active = 1');
            } 
			
			/*
				End Edit
				
			*/
		}		
        $select->order('b.blog_id DESC');
		//echo $select; return;
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function count($exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('b' => $this->_prefix . 'enterprise_blog'), array('num_blog' => 'COUNT(*)'))
                        ->where('b.language = ?', $this->_lang)
                        ;
        if ($exp) {
            if (isset($exp['enterprise'])) {
                $select->where($exp['enterprise']);
            }
			/*
				Name	: Cong Ngo, Date : 10:22 28/04/2011
				Purpose : Thêm điều kiện enterprise->frontend_active
						  +/ kết với bảng enterprise và thêm điều kiện frontend_active vào
			*/
            if (isset($exp['enterprise_frontend_active'])) {
				$select->joinLeft(array('e' => $this->_prefix.'enterprise'), "e.id = md.enterprise_id", array("id"));
                $select->where('e.frontend_active=1');
            }
			/*
				End Edit
			*/
        }
        return $select->query()->fetch()->num_blog;
    }

    public function getByIdNLang($exp = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('b' => $this->_prefix . 'enterprise_blog'))
                        ;
        if ($exp) {
            if (isset($exp['blog_id'])) {
                $select->where('b.blog_id = ?', $exp['blog_id']);
            }
            if (isset($exp['language'])) {
                $select->where('b.language = ?', $exp['language']);
            }
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function getById($id, $lang = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('b' => $this->_prefix . 'enterprise_blog'))
                        ->where('b.blog_id = ?', $id);
            if(null != $lang){
                $select->where('b.language = ?', $lang);
            }else{
                $select->where('b.language = ?', $this->_lang);
            }
                $select->limit(1);
        $rowset = $select->query()->fetchAll();
        $blog = new Tomato_Model_RecordSet($rowset, $this);
        return (count($blog) == 0) ? null : $blog[0];
    }

    public function add($blog){
        $this->_conn->insert($this->_prefix . 'enterprise_blog',
                            array(
                                'blog_id'       => $blog->blog_id,
                                'language'      => $blog->language,
                                'title'         => $blog->title,
                                'slug'          => $blog->slug,
                                'link'          => $blog->link,
                                'content'       => $blog->content,
                                'date'          => $blog->date,
                                'enterprise_id' => $blog->enterprise_id,
                            ));
        return $this->_conn->lastInsertId($this->_prefix . 'enterprise_blog');
    }

    public function update($blog){
        $where[] = "blog_id = {$blog->blog_id}";
        $where[] = "language = '{$blog->language}'";
        return $this->_conn->update($this->_prefix . 'enterprise_blog',
                            array(
                                'title'         => $blog->title,
                                'slug'          => $blog->slug,
                                'link'          => $blog->link,
                                'content'       => $blog->content,
                            ), $where);
    }

    public function delete($id){
        $this->_conn->delete($this->_prefix . 'enterprise_blog',
                            array(
                                'blog_id = ?' => $id
                            ));
    }
}
